//
//  WSJTwoLevelPicker.m
//  TSLocateView
//
//  Created by wsj2015 on 16/4/28.
//  Copyright © 2016年 Telenav Software, Inc. All rights reserved.
//

#import "WSJTwoLevelPicker.h"

#import <YTXAnimations/UIView+YTXAnimateCSS.h>

@interface WSJTwoLevelPicker ()
@property (weak, nonatomic) IBOutlet UIPickerView *pickView;

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

@property (nonatomic,  strong) UIView *maskView;

@property (nonatomic,  strong) UIWindow *bgWindow;


@property (nonatomic,  strong) NSMutableArray *myDataArray;
@property (nonatomic,  assign) NSInteger currentOneCommpontIndex;

@end

@implementation WSJTwoLevelPicker


- (UIView*)maskView{
    if (!_maskView) {
        _maskView = [UIView new];
        _maskView.frame = [UIScreen mainScreen].bounds;
        _maskView.backgroundColor = [UIColor blackColor];
        _maskView.alpha = 0;
        UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [_maskView addGestureRecognizer:tap];
        
    }
    return _maskView;
}
- (UIWindow *)bgWindow{
    if (!_bgWindow) {
        _bgWindow =[[UIWindow  alloc] initWithFrame:[UIScreen mainScreen].bounds] ;
//        _bgWindow.frame = [UIScreen mainScreen].bounds;
       _bgWindow.windowLevel = UIWindowLevelAlert;
        _bgWindow.backgroundColor = [UIColor clearColor];
    }
    return _bgWindow;
}

- (instancetype)init{
    
    return  [[NSBundle mainBundle] loadNibNamed:@"WSJTwoLevelPicker"owner:nil options:0].lastObject;
   
}

- (void)setTitle:(NSString *)title{
    self.titleLabel.text = title;
}
- (void)awakeFromNib{
    [self.bgWindow addSubview:self.maskView];
    self.frame = CGRectMake(0, kScreenHeight, kScreenWidth, 216 + 40);
    self.pickView.delegate =self;
    self.pickView.dataSource = self;
    self.currentOneCommpontIndex = 0;
    
}

- (void)setDataArray:(NSArray *)dataArray{
    if (dataArray &&dataArray.count>0) {
        self.myDataArray =[NSMutableArray arrayWithArray:dataArray];
        [self.pickView reloadAllComponents];
        
        [self.bgWindow becomeKeyWindow];
       
        [self.bgWindow addSubview:self];
      
        [self.bgWindow makeKeyAndVisible];
        weakSelf_define;
        [UIView animateWithDuration:.35f animations:^{
            weakSelf.maskView.alpha =.6;
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:.35f animations:^{
              
               weakSelf.frame =CGRectMake(0, kScreenHeight- 216 -40, kScreenWidth, 216 + 40);
            } completion:^(BOOL finished) {
                [weakSelf.pickView ytx_flipInYAnimtionWithDurationTime:.34f];
                [weakSelf.pickView reloadAllComponents];
            }];
        }];
    }
}

#pragma mark - PickerView lifecycle

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    NSInteger num = 0;
    switch (component) {
        case 0:
             num= self.myDataArray.count;
            break;
        case 1:
            if (self.myDataArray.count>0) {

                 num = [self.myDataArray[self.currentOneCommpontIndex][@"drugs"] count];
            }
            break;
        default:
            return 0;
            break;
    }

    return num;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    switch (component) {
        case 0:
            return  self.myDataArray[row][@"name"];
            break;
        case 1:{
            NSString *hehe = self.myDataArray[self.currentOneCommpontIndex][@"drugs"][row][@"name"];
            NSLog(@"%@",hehe);
            return self.myDataArray[self.currentOneCommpontIndex][@"drugs"][row][@"name"];
        }
            break;
        default:
            return nil;
            break;
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    switch (component) {
        case 0:
            self.currentOneCommpontIndex = row;
            [self.pickView selectRow:0 inComponent:1 animated:NO];
            break;
        case 1:
            //[self.pickView reloadComponent:1];
            
            break;
        default:
            break;
    }
    [self.pickView reloadComponent:1];
}

- (IBAction)saveAction:(id)sender {

    NSInteger firstIndex = [self.pickView selectedRowInComponent:0];
    NSInteger secondIndex = [self.pickView selectedRowInComponent:1];
    if ([self.myDataArray[self.currentOneCommpontIndex][@"drugs"] count] > secondIndex && self.myDataArray.count > firstIndex) {
    
        self.callBack(firstIndex, secondIndex);

    }
    [self dismiss];
    
}
- (IBAction)cancelAction:(id)sender {
    [self dismiss];
}

- (void)tapAction:(UITapGestureRecognizer *)g{
    
    [self dismiss];

}
- (void)dismiss{
    
    weakSelf_define;
    [UIView animateWithDuration:.35f animations:^{
         weakSelf.maskView.alpha =0;
        
    } completion:^(BOOL finished) {
        
        [UIView animateWithDuration:.35f animations:^{
//           weakSelf.pickView.frame =CGRectMake(0, kScreenHeight, kScreenWidth, 216 + 40);
            
            [weakSelf.pickView ytx_zoomOutAnimtionWithDurationTime:.35f];
               weakSelf.alpha = 0;
        } completion:^(BOOL finished) {
           [weakSelf.bgWindow resignKeyWindow];
            [weakSelf removeFromSuperview];
            
        }];
    }];
    
   
}

@end
